<template>
	<view>
		<swiper circular="true" :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
			<swiper-item v-for="item in bannerList" :key="item.bannerid">
				<image class="bannerImg" :src="item.img" />
			</swiper-item>
		</swiper>
		<uni-grid :column="5" :highlight="true" @change="change">
			<uni-grid-item v-for="(item, index) in navList" :index="index" :key="item.navid">
				<view class="grid-item-box" style="background-color: #fff;">
					<image class="gridImage" :src="item.imgurl" />
					<text class="text">{{ item.title }}</text>
				</view>
			</uni-grid-item>
		</uni-grid>
		<view class="proList">
			<view class="proItem" v-for="item of proList" :key="item.proid">
				<!-- <navigator :url="'/pages/detail/detail?proid=' + item.proid">
				<view class="itemImage">
					<image :src="item.img1" />
				</view>
				<view class="itemInfo">
					<view class="title ">{{ item.proname }}</view>
					<view class="price"> ￥{{ item.originprice }}</view>
				</view>
				</navigator> navigator自带跳转 -->
				<view class="itemImage" @click="toDetail(item.proid)">
					<image :src="item.img1" />
					<!-- <image :src="item.img1" mode="widthFix" />		支付宝 -->
				</view>
				<view class="itemInfo">
					<view class="title ">{{ item.proname }}</view>
					<view class="price"> ￥{{ item.originprice }}</view>
				</view>
			</view>
		</view>
		<view class="backTop" @click="backTop">
			<uni-icons type="arrow-up" size="30"></uni-icons>
		</view>
	</view>
</template>

<script setup>
	import { ref } from 'vue'
	import { onLoad, onReachBottom, onPageScroll } from '@dcloudio/uni-app'
	import navList from '../../utils/nav.js'
	import { getBannerListData, getProListData } from '../../api/home.js'

	const bannerList = ref([])
	const proList = ref([])
	const count = ref(2)
	const scrollTopVal = ref(0)
	
	const backTop = () => {
		uni.pageScrollTo({
			scrollTop: 0,
			duration: 500
		})
	}
	
	const toDetail = (proid) => {
		uni.navigateTo({
			url: '/pages/detail/detail?proid=' + proid
		})
	}

	onLoad(() => {
		getBannerListData().then(res => {
			console.log('banner', res.data.data)
			bannerList.value = res.data.data
		})
		getProListData().then(res => {
			console.log('pro', res.data.data)
			proList.value = res.data.data
		})
	})
	
	onReachBottom(() => {
		getProListData({ count: count.value }).then(res => {
			console.log('pro', res.data.data)
			if(res.data.data.length === 0) {
				uni.showToast({
					title: '没有更多数据了',
					icon: 'none'				})
			} else {
				proList.value = [...proList.value, ...res.data.data]
				count.value += 1
			}
			
		})
	}) 
</script>

<style>
	.bannerImg {
		width: 100%;
	}

	.grid-item-box {
		height: 80px;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	.grid-item-box .gridImage {
		width: 44px;
		height: 44px;
	}

	.grid-item-box .text {
		font-size: 12px;
	}

	.proList {
		width: 100%;
		display: flex;
		flex-wrap: wrap;
	}

	.proList .proItem {
		width: 48%;
		height: 260px;
		background-color: #fff;
		border-radius: 10px;
		overflow: hidden;
		margin: 5px 1%;
	}

	.proList .proItem .itemImage {
		width: 100%;
		height: 180px;
	}

	.proList .proItem .itemImage image {
		width: 100%;
		height: 180px;
	}

	.proList .proItem .itemInfo {
		padding: 8px 5px;
	}

	.proList .proItem .itemInfo .title {
		font-weight: bold;
		display: -webkit-box;
		overflow: hidden;
		text-overflow: ellipsis;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
	}

	.proList .proItem .itemInfo .price {
		color: #ff6666;
	}
	
	.backTop {
	  position: fixed;
	  right: 10px;
	  bottom: 10px;
		/* https://uniapp.dcloud.net.cn/tutorial/platform.html */
		/* #ifdef  H5  */
	  bottom: 60px;
		/* #endif */
	  width: 32px;
	  height: 32px;
	  background-color: #ccc;
	  border-radius: 50%;
	  display: flex;
	  justify-content: center;
	  align-items: center;
	}
</style>